﻿using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElasticSearchCore
{
    public static class ElasticClientExtension
    {
        public static bool CreateIndex<T>(this ElasticClient elasticClient, string indexName = "",
            int numberOfShards = 5, int numberOfReplicas = 1) where T : class
        {
            if (string.IsNullOrEmpty(indexName))
            {
                indexName = typeof(T).Name;
            }

            if (elasticClient.Indices.Exists(indexName).Exists)
            {
                return false;
            }

            var indexState = new IndexState()
            {
                Settings = new IndexSettings()
                {
                    NumberOfShards = numberOfShards,
                    NumberOfReplicas = numberOfReplicas,
                }
            };

            var response = elasticClient.Indices.Create(indexName, p => p.InitializeUsing(indexState).Map<T>(p => p.AutoMap()));
            return response.Acknowledged;
        }
    }
}
